package com.softtown.modules.sys.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.softtown.common.enums.YesOrNoEnum;
import com.softtown.common.service.impl.BaseServiceImpl;
import com.softtown.modules.organization.dto.CompSysParamIsRequiredDTO;
import com.softtown.modules.security.user.SecurityUser;
import com.softtown.modules.sys.dao.CompSysParamDao;
import com.softtown.modules.sys.dto.CompSysParamDTO;
import com.softtown.modules.sys.dto.CompSysParamPageDTO;
import com.softtown.modules.sys.entity.CompSysParamEntity;
import com.softtown.modules.sys.enums.StatusEnum;
import com.softtown.modules.sys.service.CompSysParamService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @author: Dingyayong
 * @Date: 2019/7/30 17:55
 * @Description:
 */
@Service
public class CompSysParamServiceImpl extends BaseServiceImpl<CompSysParamDao, CompSysParamEntity> implements CompSysParamService {

    @Override
    public CompSysParamEntity findItemMaxLevel(Long compId) {
        return baseDao.findItemMaxLevel(compId);
    }

    @Override
    public CompSysParamEntity findCurrValue(Long compId) {
        return baseDao.findCurrValue(compId);
    }

    /**
     * @param @param [companyId]
     * @return @return com.softtown.modules.sys.entity.CompSysParamEntity
     * @throws
     * @Title: findExpClctCdIsReqTrvlExpPlanValue
     * @Description: 通过公司ID来查找费用归集码必须参数值Service实现方法
     * @author fuming
     * @date 2019/8/25 21:13
     */
    @Override
    public CompSysParamEntity findExpClctCdIsReqTrvlExpPlanValue(Long companyId) {
        return baseDao.findExpClctCdIsReqTrvlExpPlanValue(companyId);
    }

    /**
     * @param @param [companyId]
     * @return @return com.softtown.modules.sys.entity.CompSysParamEntity
     * @throws
     * @Title: findTodoIsReqTrvlExpPlanValue
     * @Description: 通过公司ID来查找行程明细必须参数值Service实现方法
     * @author fuming
     * @date 2019/8/25 21:13
     */
    @Override
    public CompSysParamEntity findTodoIsReqTrvlExpPlanValue(Long companyId) {
        return baseDao.findTodoIsReqTrvlExpPlanValue(companyId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addCompParam(CompSysParamDTO compSysParamDTO) {
        CompSysParamEntity compSysParamEntity = new CompSysParamEntity();
        compSysParamEntity.setCompId(compSysParamDTO.getCompId());
        compSysParamEntity.setParamCode(compSysParamDTO.getParamCode());
        compSysParamEntity.setParamName(compSysParamDTO.getParamName());
        compSysParamEntity.setParamValue(compSysParamDTO.getParamValue());
        compSysParamEntity.setParamType(StatusEnum.ACTIVATESTATUS.value());
        baseDao.insert(compSysParamEntity);
    }

    @Override
    public CompSysParamEntity checkExpenseAppl(CompSysParamDTO compSysParamDTO) {
        return baseDao.checkExpenseAppl(compSysParamDTO.getCompId());
    }

    /**
     * 获取差旅报销前出差申请是否必须
     *
     * @param compSysParamIsRequiredDTO 公司参数是否必填DTO
     * @return Boolean
     * @author Dingyayong
     * @date 2019-12-26 11:30:08
     */
    @Override
    public Boolean getTrvlIsRequired(CompSysParamIsRequiredDTO compSysParamIsRequiredDTO) {
        CompSysParamEntity compSysParamEntity = baseDao.findValueByCompAndCode(compSysParamIsRequiredDTO.getCompId(), "TRVL_EXP_PLAN_IS_REQ");
        if (compSysParamEntity.getParamValue().equals(YesOrNoEnum.YES.value())) {
            return true;
        }
        return false;
    }

    /**
     * 获取差旅报销前费用归集码是否必须
     *
     * @param compSysParamIsRequiredDTO 公司参数是否必填DTO
     * @return Boolean
     * @author Dingyayong
     * @date 2019-12-26 11:30:08
     */
    @Override
    public Boolean getTrvlActClcdIsRequired(CompSysParamIsRequiredDTO compSysParamIsRequiredDTO) {
        CompSysParamEntity compSysParamEntity = baseDao.findValueByCompAndCode(compSysParamIsRequiredDTO.getCompId(), "EXP_CLCT_CD_IS_REQ_TRVL_EXP_ACT");
        if (compSysParamEntity.getParamValue().equals(YesOrNoEnum.YES.value())) {
            return true;
        }
        return false;
    }

    /**
     * 获取费用报销前事前申请单是否必须
     *
     * @param compSysParamIsRequiredDTO 公司参数是否必填DTO
     * @return Boolean
     * @author Dingyayong
     * @date 2019-12-26 11:30:08
     */
    @Override
    public Boolean getGenlActIsRequired(CompSysParamIsRequiredDTO compSysParamIsRequiredDTO) {
        CompSysParamEntity compSysParamEntity = baseDao.findValueByCompAndCode(compSysParamIsRequiredDTO.getCompId(), "GENL_EXP_PLAN_IS_REQ");
        if (compSysParamEntity.getParamValue().equals(YesOrNoEnum.YES.value())) {
            return true;
        }
        return false;
    }

    /**
     * 获取费用报销前费用归集码是否必须
     *
     * @param compSysParamIsRequiredDTO 公司参数是否必填DTO
     * @return Boolean
     * @author Dingyayong
     * @date 2019-12-26 11:30:08
     */
    @Override
    public Boolean getGenlActClcdIsRequired(CompSysParamIsRequiredDTO compSysParamIsRequiredDTO) {
        CompSysParamEntity compSysParamEntity = baseDao.findValueByCompAndCode(compSysParamIsRequiredDTO.getCompId(), "EXP_CLCT_CD_IS_REQ_GENL_EXP_ACT");
        if (compSysParamEntity.getParamValue().equals(YesOrNoEnum.YES.value())) {
            return true;
        }
        return false;
    }

    @Override
    public CompSysParamEntity checkApplBySelf(CompSysParamDTO compSysParamDTO) {
        return baseDao.checkApplBySelf(compSysParamDTO.getCompId());
    }

    @Override
    public CompSysParamEntity getExpClctCdExpense(CompSysParamDTO compSysParamDTO) {
        return baseDao.getExpClctCdExpense(compSysParamDTO.getCompId());
    }

    @Override
    public CompSysParamEntity getGenlExpActTiMing(CompSysParamDTO compSysParamDTO) {
        return baseDao.getGenlExpActTiMing(compSysParamDTO.getCompId());
    }

    @Override
    public CompSysParamEntity getGenlExpActTiMing(Long compId) {
        return baseDao.getGenlExpActTiMing(compId);
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void editCompParam(CompSysParamDTO compSysParamDTO) {
        CompSysParamEntity compSysParamEntity = baseDao.findValueByCompAndCode(compSysParamDTO.getCompId(), compSysParamDTO.getParamCode());
        compSysParamEntity.setParamValue(compSysParamDTO.getParamValue());
        compSysParamEntity.setUpdateDate(new Date());
        compSysParamEntity.setUpdater(SecurityUser.getUserId());
        compSysParamEntity.setUpdateTimestamp(String.valueOf(System.currentTimeMillis()));
        compSysParamEntity.setIsDeleted(StatusEnum.FORBIDDENSTATUS.value());
        baseDao.updateCompParam(compSysParamEntity);
    }

    @Override
    public CompSysParamEntity findValueByCompAndCode(Long compId, String paramCode) {
        return baseDao.findValueByCompAndCode(compId, paramCode);
    }

    @Override
    public PageInfo<CompSysParamEntity> getCompParamsList(CompSysParamPageDTO compSysParamPageDTO) {
        Integer pageSize = compSysParamPageDTO.getPageSize();
        Integer page = compSysParamPageDTO.getPage();
        if (pageSize > 0 && page > 0) {
            PageHelper.startPage(page, pageSize);
        }
        List<CompSysParamEntity> compSysParamEntities = baseDao.getCompParamsList(compSysParamPageDTO.getCompId(), compSysParamPageDTO.getParam());
        return new PageInfo<>(compSysParamEntities);
    }

    /**
     * 获取费用申请前费用归集码是否必须
     *
     * @param compSysParamIsRequiredDTO 公司参数是否必填DTO
     * @return java.lang.Boolean
     * @Author fuming
     * @Date 2019/12/30 11:01
     */
    @Override
    public Boolean getGenlPlanClcdIsRequired(CompSysParamIsRequiredDTO compSysParamIsRequiredDTO) {
        CompSysParamEntity compSysParamEntity = baseDao.findValueByCompAndCode(compSysParamIsRequiredDTO.getCompId(), "EXP_CLCT_CD_IS_REQ_GENL_EXP_PLAN");
        if (compSysParamEntity.getParamValue().equals(YesOrNoEnum.YES.value())) {
            return true;
        }
        return false;
    }

    @Override
    public Boolean getSpdPayClctCdIsRequired(CompSysParamIsRequiredDTO compSysParamIsRequiredDTO) {
        CompSysParamEntity compSysParamEntity = baseDao.findValueByCompAndCode(compSysParamIsRequiredDTO.getCompId(), "EXP_CLCT_CD_IS_REQ_PAY_APPL");
        if (compSysParamEntity.getParamValue().equals(YesOrNoEnum.YES.value())) {
            return true;
        }
        return false;
    }

    @Override
    public Boolean getRequiredPay(CompSysParamIsRequiredDTO compSysParamIsRequiredDTO) {
        CompSysParamEntity compSysParamEntity = baseDao.findValueByCompAndCode(compSysParamIsRequiredDTO.getCompId(), "INV_IS_REQ_PAY_APPL");
        if (compSysParamEntity.getParamValue().equals(YesOrNoEnum.YES.value())) {
            return true;
        }
        return false;
    }
}
